/*   Valhalla MailChecker -- Simple IMAP Mail Checker
*    Copyright (C) 2010-2011  Guillaume Florimond (gflorimond at gmail dot com)
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * Notification.java
 *
 * Created on 25 mai 2010, 15:04:38
 */

package fr.valhalla.mailcheck.gui;

import com.sun.awt.AWTUtilities;
import fr.valhalla.mailcheck.utils.WindowUtils;
import java.awt.Shape;
import java.awt.Window;
import java.awt.geom.Ellipse2D;
import java.lang.reflect.Method;

/**
 *
 * @author guillaume
 */
public class Notification extends javax.swing.JFrame implements Runnable {

    private static final int persistency = 10;
    private static final float opacity = 0.85f; // 0f..1f

    private String accountName;
    private String from;
    private String datetime;
    private String subject;

    /** Creates new form Notification */
    public Notification(String accountName, String from, String datetime, String subject) {
        this.accountName = accountName;
        this.from = from;
        this.datetime = datetime;
        this.subject = subject;

        initComponents();

        accountLabel.setText(accountName);
        fromLabel.setText(from + " [" + datetime + "]");
        subjectLabel.setText(subject);

        progressBar.setMaximum(persistency);
        progressBar.setMinimum(0);
        progressBar.setValue(persistency);
    }

    @Override
    @SuppressWarnings({"static-access", "empty-statement"})
    public void run() {

        // Transparency
        try {
            Class<?> awtUtilitiesClass = Class.forName("com.sun.awt.AWTUtilities");
            Method mSetWindowOpacity = awtUtilitiesClass.getMethod("setWindowOpacity", Window.class, float.class);
            mSetWindowOpacity.invoke(null, this, opacity);
        } catch (Exception e) {
            System.out.println("WARNING : Windows transparency not supported on this system.");
        }

        // Shape
        /*
        try {
            Shape shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());
            AWTUtilities.setWindowShape(this, shape);
        } catch (Exception e) {
            System.out.println("WARNING : Windows shape not supported on this system.");
        }
        */

        // Position
        WindowUtils.positionFrameOnScreen(this, 1, 0); // right,top

        // Visible
        setVisible(true);

        // Wait...
        try {
            for(int i = 0 ; i < persistency ; i++) {
                progressBar.setValue(persistency - i);
                Thread.currentThread().sleep(1000);
            }
            
        } catch(Exception e) { ; }

        // Close
        dispose();
        
    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        newMessageLabel = new javax.swing.JLabel();
        closeButton = new javax.swing.JButton();
        accountLabel = new javax.swing.JLabel();
        subjectLabel = new javax.swing.JLabel();
        fromLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);
        setUndecorated(true);

        newMessageLabel.setFont(new java.awt.Font("Lucida Grande", 1, 18)); // NOI18N
        newMessageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fr/valhalla/mailcheck/resources/images/Email.png"))); // NOI18N
        newMessageLabel.setText("Nouveau message !");

        closeButton.setText("Fermer");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        accountLabel.setText("[Account]");

        subjectLabel.setText("[Subject]");

        fromLabel.setText("[From + Date]");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(progressBar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 507, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(accountLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 411, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(subjectLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 411, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(fromLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 411, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(newMessageLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 300, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(closeButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(closeButton)
                    .add(newMessageLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(accountLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(subjectLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(fromLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(progressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 12, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
        dispose();
    }//GEN-LAST:event_closeButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel accountLabel;
    private javax.swing.JButton closeButton;
    private javax.swing.JLabel fromLabel;
    private javax.swing.JLabel newMessageLabel;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel subjectLabel;
    // End of variables declaration//GEN-END:variables

}
